<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="手机号" prop="organizationName">
                <el-input v-model="queryParams.organizationName" placeholder="请输入手机号" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="姓名" prop="canalFrom">
                <el-input v-model="queryParams.organizationName" placeholder="请输入姓名" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                <el-button type="success" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['decor:organization:add']">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="organizationList" row-key="couponId">
            <el-table-column label="ID" :show-overflow-tooltip="true" align="center" prop="couponId" />
            <el-table-column label="手机号" :show-overflow-tooltip="true" align="center" prop="couponName">
            </el-table-column>
            <el-table-column label="姓名" :show-overflow-tooltip="true" align="center" prop="couponFrom">
            </el-table-column>
            <el-table-column label="所属渠道" :show-overflow-tooltip="true" align="center" prop="couponFrom">
            </el-table-column>
            <el-table-column label="创建日期" :show-overflow-tooltip="true" align="center" prop="couponName">
            </el-table-column>
            <el-table-column label="状态" align="center" width="100">
                <template #default="scope">
                    <el-switch v-model="scope.row.status" inline-prompt active-value="0" inactive-value="1" active-text="启用"
                        inactive-text="停用" @click="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template #default="scope">
                    <el-button link type="primary" size="small" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['decor:organization:edit']">修改</el-button>
                    <el-button link type="warning" size="small" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['decor:organization:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
        <el-dialog v-model="dialogVisible" :title="title" center>
            <el-form ref="formRef" :model="form" :rules="rules" label-position="left" label-width="auto"
                class="demo-ruleForm platform-formList">
                <el-form-item label="姓名" prop="userName">
                    <el-input v-model="form.userName" placeholder="请输入姓名" @focus="userNameLimit = true"
                        @blur="userNameLimit = false" :show-word-limit="userNameLimit" maxlength="20" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="form.phone" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="所属渠道" prop="canalFrom">
                    <el-select v-model="form.canalFrom" placeholder="请选择所属渠道">
                        <el-option v-for="item in canalFromSel" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="dialogConfirm">确 定</el-button>
                    <el-button @click="dialogVisible = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { listStaffMember, getStaffMember, delStaffMember, updateStaffMember } from "@/api/decor/staffMembers";
const { proxy } = getCurrentInstance();
const router = useRouter();
const { organization_status } = proxy.useDict('organization_status');
const canalFromSel = ref([
    { value: 0, label: '平台' },
    { value: 1, label: '机构' },
    { value: 2, label: '材料商' }
])
const dialogVisible = ref(false)
const userNameLimit = ref(false)
let title = ref('')
// 表格数据
const organizationList = ref([{ id: 1 }]);
// 遮罩层
const loading = ref(false);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);

const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        organizationName: null,
        contactInformation: null,
        // orderByColumn: 'couponId',
        // isAsc: 'desc'
    },
    form: {},
    rules: {
        userName: [
            { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        canalFrom: [
            { required: true, message: "所属渠道不能为空", trigger: "blur" }
        ],
        phone: [
            { required: true, message: "手机号不能为空", trigger: "blur" },
            {
                pattern: /^1[3|4|5|6|7|8|9]\d{9}$/,
                message: "请输入正确的手机号",
                trigger: "change",
            },
        ],
    }
});

const { queryParams, form, rules } = toRefs(data);

// 表格数据
function getList() {
    loading.value = true;
    listStaffMember(queryParams.value).then(response => {
        organizationList.value = response.rows;
        total.value = response.total;
        loading.value = false;

    });
}

// 搜索按钮操作
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}
// 表单重置
function reset() {
    form.value = {
        classId: null,
        className: null,
        tenantId: null,
        parentId: null,
        ancestors: null,
        status: null,
        orderNum: null,
        version: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
    };
    proxy.resetForm("formRef");
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    dialogVisible.value = true;
    title.value = "添加工作人员信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    getStaffMember(row.classId).then(response => {
        form.value = response.data;
        dialogVisible.value = true;
        title.value = "修改工作人员信息";
    });
}
// 状态修改
function handleStatusChange(row) {
    let text = row.status === "0" ? "启用" : "停用";
    proxy.$modal.confirm('确认要"' + text + '"工作人员"' + row.roleName + '"吗?').then(function () {
        return updateStaffMember(row.couponId, row.version, row.status);
    }).then(() => {
        proxy.$modal.msgSuccess(text + "成功");
    }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
    });
}
// 删除按钮操作
function handleDelete(row) {
    const _couponIds = row.couponId;
    proxy.$modal.confirm('是否确认删除姓名为"' + row.organizationName + '"的数据？').then(function () {
        return delStaffMember(_couponIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}
// 弹框确定
const dialogConfirm = () => {
    proxy.$refs["formRef"].validate(valid => {
        if (valid) {
            updateRemit(form.value).then(response => {
                proxy.$modal.msgSuccess("上传成功");
                router.go(-1)
            });
        } else {
            proxy.$modal.msgError('请根据提示完善必填信息')
        }
    });
}
// getList();
</script>